package com.example.weather1.utils;

//import com.sun.org.apache.xml.internal.security.algorithms.JCEMapper;
import com.example.weather1.entity.UserEntity;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.security.AlgorithmConstraints;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

public class JWTUTIL {

//    private  static  String SIGNAL = "WEATHERONE";
//
//    public static  String getToken(Map<String , String > map){
//        Calendar instance = Calendar.getInstance();
//        instance.add(Calendar.DATE, 30);
//
//        final JWTCreator.Builder builder = JWT.create();
//
//        map.forEach((k,v)->{
//            builder.withClain(k,v);
//        });
//
//        String token = builder.withExpiresAt(instance.getTime())
//                .sign(JCEMapper.Algorithm.HMAC256(SIGNAL));
//
//        return token;
//
//    }


//    @org.junit.Test
    public static String jwt(UserEntity user){
        JwtBuilder jwtBuilder = Jwts.builder();

        long time = 1000*60*60*24*7;
        String signature = "admin";

//        System.out.println(user.getPhone());

        String token = jwtBuilder
                .setHeaderParam("typ","JWT")
                .setHeaderParam("alg","HS256")
                .claim("wname",user.getWname())
//                .claim("phone",user.getPhone())
                .claim("psd",user.getPsd())
                .setSubject("my_test")
                .setExpiration(new Date(System.currentTimeMillis()+time))
                .signWith(SignatureAlgorithm.HS256, signature)
                .compact();

        return token;
    }


}
